package com.koron.order.defect.plugin;

import com.alibaba.fastjson.JSON;
import com.koron.bean.base.Response;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.business.datadict.service.SysDataDictService;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.order.defect.bean.po.DefectBean;
import com.koron.order.defect.controller.DefectController;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName DefectWaitReviewPlugin
 * @description 梧州水司缺陷待审核选人规则
 * @Author zhouj
 * @Date 2022/1/12 18:52
 */
@Slf4j
@RequestMapping("/defect/plugin/wzss/waitReview/")
@RestController
@Api(tags = "梧州水司-缺陷工单-待审核插件")
public class WZSSDefectWaitReviewPlugin {

    @Autowired
    private WorkFlowHandler workFlowHandler;

    @Autowired
    private DefectController defectController;

    @Autowired
    private SysDataDictService sysDataDictService;

    @Autowired
    private SystemFeignService systemFeignService;

    //设备管理单位领导审核 节点配置出口规则
    @PostMapping("/task1/submitter")
    public Response<List<WorkflowUser>> submitter1(@RequestBody SubmitterParam submitterParam) {

        String formVariables = submitterParam.getFormVariables();
        List<WorkflowUser> workflowUsers = submitterParam.getUsers();
        //返回对象
        List<WorkflowUser> result = new ArrayList<>();

        DefectBean defectBean = getBussinessBean(submitterParam);
        /*
                节点1的过滤处理  设备管理单位领导审核
                1、处理人	厂长（岗位）、生产管理（岗位）
                2、必须变更审批人的节点	无
                3、自动过滤规则	厂长（岗位）、生产管理（岗位）
                4、操作类型	沟通、转办、驳回
                5、通知方式	待办
                6、流转方式	并行
                7、变更工作流节点	根据人工决策是否结束或转派至维修室副经理审批
                */

        //过滤岗位条件
        List<WorkflowUser> gwList = workflowUsers.stream().filter(p -> 2 == p.getType()).collect(Collectors.toList());
        Assert.notEmpty(gwList, "配置的岗位不能为空");
        //分别通过各自id/code进行查询，或者过滤，最终转化为人员
        List<String> gwIdList = gwList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());

        PostUserQuery postUserQuery = new PostUserQuery();
        postUserQuery.setPostIdList(gwIdList);
        //拿到岗位下的人
        List<PostUserVo> postUserVos = getPostList(postUserQuery);
        //转换为返回对象
        convertUser(postUserVos, result);
        return Response.ok(result);
    }

    //维修室审批 节点配置入口规则
    @PostMapping("/task2/submitter")
    public Response<List<WorkflowUser>> submitter2(@RequestBody SubmitterParam submitterParam) {
        /*
        1、处理人	副经理（维修室）（岗位）、班长（岗位）
        2、自动过滤规则	自动发给副经理（维修室）、维修室的班长（岗位）
        3、操作类型	沟通、转办、驳回
        4、通知方式	待办
        5、流转方式	会审
        6、变更工作流节点	根据人工决策是否结束或转派至生产部审批
        */
        List<WorkflowUser> workflowUsers = submitterParam.getUsers();
        //主要根据专业来筛选
        DefectBean defectBean = getBussinessBean(submitterParam);

        List<WorkflowUser> result = new ArrayList<>();

        /*name code type*/
        //先分类
        List<WorkflowUser> gwList = workflowUsers.stream().filter(p -> 2 == p.getType()).collect(Collectors.toList());

        List<WorkflowUser> orgList = workflowUsers.stream().filter(p -> 1 == p.getType()).collect(Collectors.toList());

        PostUserQuery postUserQuery = new PostUserQuery();
        Assert.notEmpty(gwList, "配置的岗位不能为空");
        //分别通过各自id/code进行查询，或者过滤，最终转化为人员
        List<String> gwIdList = gwList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());
        postUserQuery.setPostIdList(gwIdList);

        if (!CollectionUtils.isEmpty(orgList)) {
            List<String> orgIdList = orgList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());
            postUserQuery.setParentOrgIdList(orgIdList);
        }
        //拿到岗位下的人
        List<PostUserVo> postUserVos = getPostList(postUserQuery);
        convertUser(postUserVos, result);
        return Response.ok(result);
    }

    //生产部审批 节点配置入口规则
    @PostMapping("/task3/submitter")
    public Response<List<WorkflowUser>> submit3(@RequestBody SubmitterParam submitterParam) {
        //节点3的过滤处理

        /*
        1、处理人	经理（岗位）
        2、自动过滤规则	自动发给生产部的经理
        3、操作类型	沟通、转办、驳回
        4、通知方式	待办
        5、流转方式	并行
        6、变更工作流节点	根据人工决策是否结束或转派至安监技术部审批
        */

        List<WorkflowUser> workflowUsers = submitterParam.getUsers();
        //主要根据专业来筛选
        DefectBean defectBean = getBussinessBean(submitterParam);

        List<WorkflowUser> result = new ArrayList<>();

        //先分类
        List<WorkflowUser> gwList = workflowUsers.stream().filter(p -> 2 == p.getType()).collect(Collectors.toList());
        List<WorkflowUser> orgList = workflowUsers.stream().filter(p -> 1 == p.getType()).collect(Collectors.toList());

        Assert.notEmpty(gwList, "配置的岗位不能为空");

        PostUserQuery postUserQuery = new PostUserQuery();
        //通过过滤条件进行查询，或者过滤，最终转化为人员
        List<String> gwIdList = gwList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());
        postUserQuery.setPostIdList(gwIdList);
        if (!CollectionUtils.isEmpty(orgList)) {
            List<String> orgIdList = orgList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());
            postUserQuery.setParentOrgIdList(orgIdList);
        }

        //拿到岗位下的人
        List<PostUserVo> postUserVos = getPostList(postUserQuery);
        convertUser(postUserVos, result);
        return Response.ok(result);

    }

    //生产部审批 节点配置入口规则
    @PostMapping("/task4/submitter")
    public Response<List<WorkflowUser>> submit4(@RequestBody SubmitterParam submitterParam) {
        //节点3的过滤处理

        /*
        1、处理人	副经理（岗位），机电管理（岗位）
        2、自动过滤规则	自动发送给安监技术部副经理（岗位），安监技术部的机电管理（岗位）
        3、操作类型	沟通、转办、驳回
        4、通知方式	待办
        5、流转方式	并行
        */

        List<WorkflowUser> workflowUsers = submitterParam.getUsers();
        //主要根据专业来筛选
        DefectBean defectBean = getBussinessBean(submitterParam);

        List<WorkflowUser> result = new ArrayList<>();

        //先分类
        List<WorkflowUser> gwList = workflowUsers.stream().filter(p -> 2 == p.getType()).collect(Collectors.toList());
        List<WorkflowUser> orgList = workflowUsers.stream().filter(p -> 1 == p.getType()).collect(Collectors.toList());

        Assert.notEmpty(gwList, "配置的岗位不能为空");

        PostUserQuery postUserQuery = new PostUserQuery();
        //通过过滤条件进行查询，或者过滤，最终转化为人员
        List<String> gwIdList = gwList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());
        postUserQuery.setPostIdList(gwIdList);
        if (!CollectionUtils.isEmpty(orgList)) {
            List<String> orgIdList = orgList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());
            postUserQuery.setParentOrgIdList(orgIdList);
        }

        //拿到岗位下的人
        List<PostUserVo> postUserVos = getPostList(postUserQuery);
        convertUser(postUserVos, result);
        return Response.ok(result);

    }

    private DefectBean getBussinessBean(SubmitterParam submitterParam) {
        return JSON.parseObject(submitterParam.getFormVariables(), DefectBean.class);
    }

    private List<PostUserVo> getPostList(PostUserQuery postUserQuery) {
        Response<List<PostUserVo>> listResponse = systemFeignService.queryPostList(postUserQuery);
        List<PostUserVo> postUserVos = listResponse.getData();
        return postUserVos;
    }

    private void convertUser(List<PostUserVo> postUserVos, List<WorkflowUser> result) {
        //转换为返回对象
        if (CollectionUtils.isEmpty(postUserVos)) {
            return;
        }
        for (PostUserVo postUserVo : postUserVos) {
            WorkflowUser workflowUser = new WorkflowUser();
            workflowUser.setCode(postUserVo.getUserAccount());
            workflowUser.setName(postUserVo.getUserName());
            result.add(workflowUser);
        }
    }

}
